package edu.bu.bga;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import edu.bu.bga.db.DatabaseHelper;

public class SettingsActivity extends Activity {

	protected static final int MIN_CHARS = 6;
	protected static final int MAX_CHARS = 15;

	private Button backButton;
	private Button resetButton;
	private Button addRuleButton;
	private Button deleteRuleButton;

	private DatabaseHelper databaseHelper;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingslayout);

		databaseHelper = new DatabaseHelper(getApplicationContext());

		backButton = (Button) this.findViewById(R.id.backbutton);
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		resetButton = (Button) this.findViewById(R.id.resetrulesbutton);
		resetButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				databaseHelper.resetTables();
				Toast.makeText(getBaseContext(), "Reset tables",
						Toast.LENGTH_LONG).show();

			}
		});

		addRuleButton = (Button) this.findViewById(R.id.addrulebutton);
		addRuleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showAddDialog();
			}
		});

		deleteRuleButton = (Button) this.findViewById(R.id.deleterulebutton);
		deleteRuleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getBaseContext(),
						DeleteRuleActivity.class);
				startActivity(intent);
			}
		});
	}

	private void showAddDialog() {

		final Dialog loginDialog = new Dialog(this);
		loginDialog.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		loginDialog.setTitle("Add New Rule");

		LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View dialogView = li.inflate(R.layout.add_rule, null);
		loginDialog.setContentView(dialogView);

		TextView newRuleInstructionsTextBox = (TextView) dialogView
				.findViewById(R.id.add_rule_instructions);

		newRuleInstructionsTextBox.setText("Type a rule between " + MIN_CHARS
				+ " and " + MAX_CHARS + " characters in length.");

		loginDialog.show();

		Button addButton = (Button) dialogView.findViewById(R.id.add_button);
		Button cancelButton = (Button) dialogView
				.findViewById(R.id.cancel_button);

		addButton.setOnClickListener(new OnClickListener() {

			/**
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			public void onClick(View v) {

				EditText newRuleTextBox = (EditText) dialogView
						.findViewById(R.id.new_rule_text);

				String newRuleText = newRuleTextBox.getText().toString();

				// go through validation
				if (newRuleText == null || newRuleText.length() == 0) {
					Toast.makeText(getBaseContext(), "Can't add a blank rule",
							Toast.LENGTH_LONG).show();
					return;
				}

				// trim off any white space
				newRuleText = newRuleText.trim();

				if (newRuleText.length() < MIN_CHARS + 1) {
					Toast.makeText(
							getBaseContext(),
							"Can't add a rule shorter than " + MIN_CHARS
									+ "  characters", Toast.LENGTH_LONG).show();
					return;
				}

				if (newRuleText.length() > MAX_CHARS) {
					Toast.makeText(
							getBaseContext(),
							"Can't add a rule longer than " + MAX_CHARS
									+ "  characters", Toast.LENGTH_LONG).show();
					return;
				}

				// check if rule text is alphanumeric

				// if we've made it this far, then all validation is complete,
				// add the rule

				String finalRuleText = "Player with the " + newRuleText
						+ " goes first";

				Toast.makeText(getBaseContext(),
						"Added new rule: " + finalRuleText, Toast.LENGTH_LONG)
						.show();

				// add new rule
				databaseHelper.insertRule(finalRuleText);

				// dismiss the dialog
				loginDialog.dismiss();

			}
		});

		cancelButton.setOnClickListener(new OnClickListener() {

			/**
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			public void onClick(View v) {
				loginDialog.dismiss();
			}
		});

	}
}
